package cn.edu.sptc.sms_chensiyi.controller;

import cn.edu.sptc.sms_chensiyi.entity.Major;
import cn.edu.sptc.sms_chensiyi.entity.Student;
import cn.edu.sptc.sms_chensiyi.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //select
//    @GetMapping("/query/{id}")
//    public Major queryMajorByID(@PathVariable String id){
//        return majorService.queryByID(Long.parseLong(id));//string转换为long型
//    }

    @GetMapping("/query/{id}")
    public Map<String,Object> queryMajorByID(@PathVariable String id){
        Map map=new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",studentService.queryByID(Long.parseLong(id)));
        return map;//string转换为long型
    }

    @GetMapping("/query")
    public Map<String,Object> queryMajorByIDParam(@RequestParam String id){
        Map map=new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",studentService.queryByID(Long.parseLong(id)));
        return map;

    }

    @GetMapping("/queryAll")
    public Map<String,Object> queryMajorByall (){
        Map map=new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",studentService.queryByALl());
        return map;
    }

    @PostMapping(value = "/save",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Map<String,Object> insert(@RequestBody Student student){
        Map map=new HashMap<>();
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());
        if(studentService.insertStudent(student)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = "/save",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Map<String,Object> insertByFROM(Student student){
        Map map=new HashMap<>();
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());
        if(studentService.insertStudent(student)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }
    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Map<String,Object> updateByJSON(@RequestBody Student student){
        Map map=new HashMap<>();
        student.setUpdateTime(new Date());
        if(studentService.updateStudent(student)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Map<String,Object> updateByFROM(Student student){
        Map map=new HashMap<>();
        student.setUpdateTime(new Date());
        if(studentService.updateStudent(student)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }

    @GetMapping("/delete/{id}")
    public Map<String,Object> deleteStudentByID(@PathVariable String id){
        Map map=new HashMap();
        if(studentService.deleteStudent(Long.parseLong(id))){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;//string转换为long型
    }

    @GetMapping("/delete")
    public Map<String,Object> deleteStudentByParam(@RequestParam String id){
        Map map=new HashMap();
        if(studentService.deleteStudent(Long.parseLong(id))){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;//string转换为long型
    }

}
